<template>
  <div class="location">
    <div class="top">
      <i class="iconfont icon-fanhui fanhui" @click="goBack"></i>
      <span class="title">教学地点</span>
    </div>
    <div class="map">
      <baidu-map class="bm-view" :center="where" :zoom="zoom" @ready="handler">
      </baidu-map>
    </div>
    <div class="bottom"><i class="iconfont icon-wxbdingwei"></i>{{currentPosition.name}}(我的位置)</div>
  </div>
</template>

<script>
  export default {
    data() {
      return {
        where: '',
        zoom: 17,
        currentPosition: {
          name: '',
          lng: 0,
          lat: 0
        }
      }
    },
    created() {
      this.where = this.$route.query.where
    },
    methods: {
      goBack() {
        this.$router.go(-1)
      },
      handler({BMap, map}) {
        let _this = this;
        let geolocation = new BMap.Geolocation();
        geolocation.getCurrentPosition(function (r) {
          if (this.getStatus() == BMAP_STATUS_SUCCESS) {
            _this.currentPosition.lng = r.point.lng
            _this.currentPosition.lat = r.point.lat
            let myGeo = new BMap.Geocoder();
            // 根据坐标得到地址描述
            myGeo.getLocation(new BMap.Point(_this.currentPosition.lng, _this.currentPosition.lat), function (result) {
              if (result) {
                _this.currentPosition.name = result.address;
              }
            });
          }
          else {
            alert('failed' + this.getStatus());
          }
        });

      }
    }
  }
</script>

<style scoped>
  .location {
    position: fixed;
    width: 100%;
    height: 100%;
  }

  .top {
    width: 100%;
    height: 45px;
    text-align: center;
    line-height: 45px;
  }

  .top .fanhui {
    color: #ccc;
    font-weight: bold;
    margin-top: -18px;
  }

  .map {
    position: absolute;
    top: 45px;
    bottom: 94.6px;
    width: 100%;
  }

  .map .bm-view {
    width: 100%;
    height: 100%;
  }

  .bottom {
    position: absolute;
    width: 100%;
    bottom: 49.6px;
    height: 45px;
    line-height: 45px;
    padding-left: 15px;
    font-size: 13px;
    overflow: hidden;
    text-overflow: ellipsis;
  }
</style>
